Skip to content

Fix Expression Formatter#217

Merged
rcosta358 merged 5 commits into
mainfrom
fix-expression-formatter
May 11, 2026
Merged

Fix Expression Formatter#217
rcosta358 merged 5 commits into
mainfrom
fix-expression-formatter

Conversation

@rcosta358
Copy link
Copy Markdown
Collaborator

Description

This PR fixes the ExpressionFormatter so grouped expressions only have parentheses when needed for precedence or associativity. This simplifies expressions in the error messages and in the context debugger.

Example

Before and after:

  • (1)1
  • (a > 0)a > 0
  • a && (b > 0)a && b > 0

Unchanged cases:

  • a - (b + c)
  • x == (1 > 0)
  • !(x > 0)

Related Issue

None.

Type of change

  • Bug fix
  • New feature
  • Documentation update
  • Code refactoring

Checklist

  • Added/updated in ExpressionFormatterTest
  • mvn test passes locally
  • Updated docs/README if behavior or API changed

@rcosta358 rcosta358 requested a review from CatarinaGamboa May 10, 2026 13:40
@rcosta358 rcosta358 self-assigned this May 10, 2026
@rcosta358 rcosta358 added error messages Related to error messages enhancement New feature or request labels May 10, 2026
Copy link
Copy Markdown
Collaborator

@CatarinaGamboa CatarinaGamboa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep the ite grouped expressions, maybe the --> as well. They would pass the parser correctly but its harder for developers to easily see the right associative rule - at least for me it is, what do you think?

assertEquals("(a ? b : c) ? d : e",
new Ite(new GroupExpression(ite), new Var("d"), new Var("e")).toDisplayString());
assertEquals("a ? b : (c ? d : e)",
assertEquals("a ? b : c ? d : e",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in here I think we want the parenthesis to still be there, right? reading it, it could either be
a ? b : c ? d : e
a) a ? b : (c ? d : e)
b) (a ? b : c) ? d : e
Also we can improve the tree construction as well, but you can do that in a followup PR

@rcosta358
Copy link
Copy Markdown
Collaborator Author

I think it's understandable without the parentheses but I get your point.
Will change it.

@rcosta358 rcosta358 requested a review from CatarinaGamboa May 11, 2026 14:26
Copy link
Copy Markdown
Collaborator

@CatarinaGamboa CatarinaGamboa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you did with the examples since the last ones added () on purpose. But now its strange that some tests are just
assertEquals("x", parse("x").toDisplayString());
can we also add some with
assertEquals("x", parse("(x)").toDisplayString());

assertEquals("#fresh¹²", new Var("#fresh_12").toDisplayString());
assertEquals("#ret³", new Var("#ret_3").toDisplayString());
assertEquals("this#Class", new Var("this#Class").toDisplayString());
assertEquals("x", parse("x").toDisplayString());
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didnt you want to try assertEquals("x", parse("(x)").toDisplayString());

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep that case is missing, thanks.

@rcosta358 rcosta358 merged commit 9f1c367 into main May 11, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request error messages Related to error messages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants